﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;

namespace Gift.WebApp
{
    [Serializable]
    public class SendMailMessage
    {
        // Fields
        public string accessories;
        public string body;
        public string from;
        public string fromName;
        public bool html;
        public string mailDomain;
        public int mailDomainPort;
        public string mailServerPassWord;
        public string mailServerUserName;
        public string mailTo;
        public string subject;

        // Events
        public event SendMailHandler SendMailEvent;

        // Methods
        public bool RuleSend()
        {
            MailMessage message = new MailMessage();
            try
            {
                Encoding displayNameEncoding = Encoding.GetEncoding("utf-8");
                message.From = new MailAddress(this.from, this.fromName, displayNameEncoding);
                message.To.Add(this.mailTo);
                message.IsBodyHtml = this.html;
                message.Subject = this.subject;
                message.Body = this.body;
                if (this.accessories != null)
                {
                    Attachment item = new Attachment(this.accessories, "application/octet-stream");
                    message.Attachments.Add(item);
                }
                message.Priority = MailPriority.High;
                SmtpClient client = new SmtpClient();
                client.Timeout = 0x927c0;
                client.Host = this.mailDomain;
                client.Port = this.mailDomainPort;
                if (this.mailDomainPort != 0x19)
                {
                    client.EnableSsl = true;
                }
                client.Credentials = new NetworkCredential(this.mailServerUserName, this.mailServerPassWord);
                client.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Send()
        {
            MailMessage message = new MailMessage();
            try
            {
                Encoding displayNameEncoding = Encoding.GetEncoding("utf-8");
                message.From = new MailAddress(this.from, this.fromName, displayNameEncoding);
                message.To.Add(this.mailTo);
                message.IsBodyHtml = this.html;
                message.Subject = this.subject;
                message.Body = this.body;
                if (this.accessories != null)
                {
                    Attachment item = new Attachment(this.accessories, "application/octet-stream");
                    message.Attachments.Add(item);
                }
                message.Priority = MailPriority.High;
                SmtpClient client = new SmtpClient();
                client.Timeout = 0x927c0;
                client.Host = this.mailDomain;
                client.Port = this.mailDomainPort;
                if (this.mailDomainPort != 0x19)
                {
                    client.EnableSsl = true;
                }
                client.Credentials = new NetworkCredential(this.mailServerUserName, this.mailServerPassWord);
                client.Send(message);
                this.SendMailEvent();
            }
            catch
            {
            }
        }

        // Nested Types
        public delegate void SendMailHandler();
    }

 


}